home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 12 / Example 12.6 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  6.7 KB  |  280 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 12.6: Lens Flare Effect                            //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "skybox.h"
  11. #include "mouse.h"
  12. #include "effect.h"
  13.  
  14. class APPLICATION
  15. {
  16.     public:
  17.         APPLICATION();
  18.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  19.         HRESULT Update(float deltaTime);
  20.         HRESULT Render();
  21.         HRESULT Cleanup();
  22.         HRESULT Quit();
  23.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  24.  
  25.     private:
  26.         IDirect3DDevice9* m_pDevice; 
  27.         MOUSE m_mouse;
  28.         SKYBOX *m_pSkybox;
  29.  
  30.         HWND m_mainWindow;
  31.         INTPOINT m_lastM;
  32.         D3DXVECTOR3 m_rot;
  33.  
  34.         //Effects 
  35.         std::vector<EFFECT*> m_effects;
  36. };
  37.  
  38. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  39. {
  40.     APPLICATION app;
  41.  
  42.     if(FAILED(app.Init(hInstance, 800, 600, true)))return 0;
  43.  
  44.     MSG msg;
  45.     memset(&msg, 0, sizeof(MSG));
  46.     int startTime = timeGetTime(); 
  47.  
  48.     while(msg.message != WM_QUIT)
  49.     {
  50.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  51.         {
  52.             ::TranslateMessage(&msg);
  53.             ::DispatchMessage(&msg);
  54.         }
  55.         else
  56.         {    
  57.             int t = timeGetTime();
  58.             float deltaTime = (t - startTime)*0.001f;
  59.  
  60.             app.Update(deltaTime);
  61.             app.Render();
  62.  
  63.             startTime = t;
  64.         }
  65.     }
  66.  
  67.     app.Cleanup();
  68.  
  69.     return msg.wParam;
  70. }
  71.  
  72. APPLICATION::APPLICATION()
  73. {
  74.     m_pDevice = NULL; 
  75.     m_mainWindow = 0;
  76.     srand(GetTickCount());
  77.     m_pSkybox = NULL;
  78.     m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  79. }
  80.  
  81. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  82. {
  83.     debug.Print("Application initiated");
  84.  
  85.     //Create Window Class
  86.     WNDCLASS wc;
  87.     memset(&wc, 0, sizeof(WNDCLASS));
  88.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  89.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  90.     wc.hInstance     = hInstance;
  91.     wc.lpszClassName = "D3DWND";
  92.  
  93.     //Register Class and Create new Window
  94.     RegisterClass(&wc);
  95.     m_mainWindow = CreateWindow("D3DWND", "Example 12.6: Lens Flare Effect", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  96.     SetCursor(NULL);
  97.     ShowWindow(m_mainWindow, SW_SHOW);
  98.     UpdateWindow(m_mainWindow);
  99.  
  100.     //Create IDirect3D9 Interface
  101.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  102.  
  103.     if(d3d9 == NULL)
  104.     {
  105.         debug.Print("Direct3DCreate9() - FAILED");
  106.         return E_FAIL;
  107.     }
  108.  
  109.     //Check that the Device supports what we need from it
  110.     D3DCAPS9 caps;
  111.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  112.  
  113.     //Hardware Vertex Processing or not?
  114.     int vp = 0;
  115.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  116.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  117.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  118.  
  119.     //Check vertex & pixelshader versions
  120.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  121.     {
  122.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  123.     }
  124.  
  125.     //Set D3DPRESENT_PARAMETERS
  126.     D3DPRESENT_PARAMETERS d3dpp;
  127.     d3dpp.BackBufferWidth            = width;
  128.     d3dpp.BackBufferHeight           = height;
  129.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  130.     d3dpp.BackBufferCount            = 1;
  131.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  132.     d3dpp.MultiSampleQuality         = 0;
  133.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  134.     d3dpp.hDeviceWindow              = m_mainWindow;
  135.     d3dpp.Windowed                   = windowed;
  136.     d3dpp.EnableAutoDepthStencil     = true; 
  137.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  138.     d3dpp.Flags                      = 0;
  139.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  140.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  141.  
  142.     //Create the IDirect3DDevice9
  143.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  144.                                  vp, &d3dpp, &m_pDevice)))
  145.     {
  146.         debug.Print("Failed to create IDirect3DDevice9");
  147.         return E_FAIL;
  148.     }
  149.  
  150.     //Release IDirect3D9 interface
  151.     d3d9->Release();
  152.  
  153.     m_mouse.InitMouse(m_pDevice, m_mainWindow);
  154.     m_lastM = m_mouse;
  155.     m_lastM.y += 100;
  156.  
  157.     LoadEffectResources(m_pDevice);
  158.  
  159.     m_pSkybox = new SKYBOX(m_pDevice, "textures/skybox", 300.0f);
  160.  
  161.     m_effects.push_back(new EFFECT_LENSFLARE(m_pDevice, 0, D3DXVECTOR3(-200.0f, 290.0f, 0.0f)));
  162.  
  163.     //Set sampler state
  164.     for(int i=0;i<4;i++)
  165.     {
  166.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  167.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  168.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  169.     }
  170.  
  171.     return S_OK;
  172. }
  173.  
  174. HRESULT APPLICATION::Update(float deltaTime)
  175. {
  176.     m_mouse.Update();
  177.  
  178.     //Remove dead effects
  179.     std::vector<EFFECT*>::iterator i;
  180.     for(i=m_effects.begin();i != m_effects.end();)
  181.     {
  182.         if((*i)->isDead())
  183.         {
  184.             delete (*i);
  185.             m_effects.erase(i);
  186.         }
  187.         else 
  188.         {
  189.             (*i)->Update(deltaTime);
  190.             i++;
  191.         }
  192.     }
  193.  
  194.     //Update Camera
  195.     if(m_mouse != m_lastM)
  196.     {
  197.         m_rot.y -= (m_lastM.x - m_mouse.x) * 0.01f;
  198.         m_rot.z += (m_lastM.y - m_mouse.y) * 0.01f;
  199.         m_lastM = m_mouse;
  200.     }
  201.  
  202.     //Keayboard input
  203.     if(KEYDOWN(VK_SPACE))
  204.     {
  205.     }
  206.     if(KEYDOWN(VK_RETURN))
  207.     {
  208.     }
  209.     else if(KEYDOWN(VK_ESCAPE))
  210.     {
  211.         Quit();
  212.     }
  213.  
  214.     return S_OK;
  215. }    
  216.  
  217. HRESULT APPLICATION::Render()
  218. {
  219.     // Clear the viewport
  220.     m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  221.  
  222.     //Set camera
  223.     D3DXMATRIX r;
  224.     D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  225.     D3DXVECTOR3 focus = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
  226.     D3DXVec3TransformNormal(&focus, &focus, &r);
  227.     D3DXVec3Normalize(&focus, &focus);
  228.  
  229.     D3DXMATRIX view, proj, world, identity;
  230.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &focus, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  231.     D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.4f, 1.33333f, 0.01f, 1000.0f);
  232.     D3DXMatrixIdentity(&identity);
  233.  
  234.     m_pDevice->SetTransform(D3DTS_WORLD, &identity);
  235.     m_pDevice->SetTransform(D3DTS_VIEW, &view);
  236.     m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);
  237.  
  238.     // Begin the scene 
  239.     if(SUCCEEDED(m_pDevice->BeginScene()))
  240.     {
  241.         m_pSkybox->Render(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
  242.  
  243.         //Render Effects
  244.         for(int i=0;i<m_effects.size();i++)
  245.             if(m_effects[i] != NULL)
  246.                 m_effects[i]->Render();
  247.  
  248.         m_mouse.Paint();
  249.  
  250.         // End the scene.
  251.         m_pDevice->EndScene();
  252.         m_pDevice->Present(0, 0, 0, 0);
  253.     }
  254.  
  255.     return S_OK;
  256. }
  257.  
  258. HRESULT APPLICATION::Cleanup()
  259. {
  260.     try
  261.     {
  262.         UnloadEffectResources();
  263.  
  264.         if(m_pSkybox)delete m_pSkybox;
  265.  
  266.         m_pDevice->Release();
  267.  
  268.         debug.Print("Application terminated");
  269.     }
  270.     catch(...){}
  271.  
  272.     return S_OK;
  273. }
  274.  
  275. HRESULT APPLICATION::Quit()
  276. {
  277.     ::DestroyWindow(m_mainWindow);
  278.     ::PostQuitMessage(0);
  279.     return S_OK;
  280. }